package com.uchiha.nova.mainService.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.uchiha.nova.core.model.dto.OilUserDTO;
import com.uchiha.nova.core.model.entity.RoleEntity;
import com.uchiha.nova.core.model.entity.UserEntity;
import com.uchiha.nova.core.model.entity.UserRoleEntity;
import com.uchiha.nova.core.tool.api.R;
import com.uchiha.nova.core.tool.constant.NovaDefaultConstant;
import com.uchiha.nova.core.tool.support.BaseServiceImpl;
import com.uchiha.nova.core.model.entity.OilUserEntity;
import com.uchiha.nova.mainService.mapper.OilUserMapper;
import com.uchiha.nova.mainService.mapper.RoleMapper;
import com.uchiha.nova.mainService.service.IOilUserService;
import com.uchiha.nova.mainService.service.UserRoleService;
import com.uchiha.nova.mainService.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务人员账号 服务实现类
 *
 * @author Uchihashiamu
 */
@Service
public class OilUserServiceImpl extends BaseServiceImpl<OilUserMapper, OilUserEntity> implements IOilUserService {

    @Resource
    PasswordEncoder passwordEncoder;
    @Resource
    UserService userService;
    @Resource
    UserRoleService userRoleService;
    @Resource
    RoleMapper roleMapper;

    @Override
    public boolean deletePhysics(List<Long> ids) {
        ids.forEach(item-> this.baseMapper.deletePhysics(item));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R saveOilUser(OilUserDTO oilUserDTO) {
        String password = passwordEncoder.encode(ObjectUtil.defaultIfNull(oilUserDTO.getPassword(), NovaDefaultConstant.DEFAULT_PASSWORD));
        OilUserEntity entity = new OilUserEntity();
        BeanUtil.copyProperties(oilUserDTO, entity);
        entity.setImg(ObjectUtil.defaultIfEmpty(oilUserDTO.getImg(),NovaDefaultConstant.DEFAULT_AVATAR));
        UserEntity userEntity = new UserEntity();
        userEntity.setPassword(password);
        userEntity.setName(oilUserDTO.getName());
        userEntity.setTenantId("1111");
        userEntity.setUsername(oilUserDTO.getUsername());
        userEntity.setAvatar(ObjectUtil.defaultIfEmpty(oilUserDTO.getImg(),NovaDefaultConstant.DEFAULT_AVATAR));
        userService.save(userEntity);
        Long userId = userEntity.getId();
        UserRoleEntity userRoleEntity = new UserRoleEntity();
        userRoleEntity.setUserId(userId);
        userRoleEntity.setRoleId(oilUserDTO.getRoleId());
        userRoleService.save(userRoleEntity);
        entity.setId(userId);
        entity.setTenantId("1111");
        RoleEntity roleEntity = roleMapper.selectById(oilUserDTO.getRoleId());
        List<String> dataAuth = oilUserDTO.getDataAuth();
        if(roleEntity.getRoleName().equals("ROLE_Oil_Province")){
            entity.setProvinceCode(dataAuth.get(0));
            entity.setCityCode(null);
            entity.setAreaCode(null);
        }else if(roleEntity.getRoleName().equals("ROLE_Oil_City")){
            entity.setProvinceCode(dataAuth.get(0));
            entity.setCityCode(dataAuth.get(1));
            entity.setAreaCode(null);
        }else if(roleEntity.getRoleName().equals("ROLE_Oil_Area")){
            entity.setProvinceCode(dataAuth.get(0));
            entity.setCityCode(dataAuth.get(1));
            entity.setAreaCode(dataAuth.get(2));
        }
        return R.status(this.save(entity));
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateOilUser(OilUserDTO oilUserDTO) {
        OilUserEntity entityEx = this.getById(oilUserDTO.getId());
        UserEntity userEntity = userService.getById(oilUserDTO.getId());
        entityEx.setName(oilUserDTO.getName());
        userEntity.setName(oilUserDTO.getName());
        entityEx.setImg(ObjectUtil.defaultIfEmpty(oilUserDTO.getImg(),NovaDefaultConstant.DEFAULT_AVATAR));
        userEntity.setAvatar(oilUserDTO.getImg());
        userService.updateById(userEntity);
        if(entityEx.getRoleId().compareTo(oilUserDTO.getRoleId())!=0){
            entityEx.setRoleId(oilUserDTO.getRoleId());
            userRoleService.deleteUser(entityEx.getId());
            UserRoleEntity userRoleEntity = new UserRoleEntity();
            userRoleEntity.setUserId(entityEx.getId());
            userRoleEntity.setRoleId(oilUserDTO.getRoleId());
            userRoleService.save(userRoleEntity);
        }
        RoleEntity roleEntity = roleMapper.selectById(oilUserDTO.getRoleId());

        List<String> dataAuth = oilUserDTO.getDataAuth();
        if(roleEntity.getRoleName().equals("ROLE_Oil_Province")){
            entityEx.setProvinceCode(dataAuth.get(0));
            entityEx.setCityCode("");
            entityEx.setAreaCode("");
        }else if(roleEntity.getRoleName().equals("ROLE_Oil_City")){
            entityEx.setProvinceCode(dataAuth.get(0));
            entityEx.setCityCode(dataAuth.get(1));
            entityEx.setAreaCode("");
        }else if(roleEntity.getRoleName().equals("ROLE_Oil_Area")){
            entityEx.setProvinceCode(dataAuth.get(0));
            entityEx.setCityCode(dataAuth.get(1));
            entityEx.setAreaCode(dataAuth.get(2));
        }
        this.updateById(entityEx);
//        this.baseMapper.update(lambdaUpdate);
        return R.status(true);
    }
}
